import tkinter as tk
from tkinter import ttk
import threading
import time
from switchnet import SwitcheNet


class SwitchButton(tk.Canvas):
    def __init__(
        self,
        parent,
        width=60,
        height=30,
        bg="#ffffff",
        fg_off="#cccccc",
        fg_on="#4CAF50",
        circle_off="#f5f5f5",
        circle_on="#ffffff",
        command=None,
    ):
        super().__init__(
            parent, width=width, height=height, bg=bg, highlightthickness=0
        )
        self.width = width
        self.height = height
        self.bg = bg
        self.fg_off = fg_off
        self.fg_on = fg_on
        self.circle_off = circle_off
        self.circle_on = circle_on
        self.command = command
        self.state = False

        # Draw the initial switch
        self.draw_switch()

        # Bind click event
        self.bind("<Button-1>", self.toggle)

    def draw_switch(self):
        self.delete("all")

        # Draw the background rectangle
        if self.state:
            bg_color = self.fg_on
            circle_x = self.width - self.height / 2
            circle_color = self.circle_on
        else:
            bg_color = self.fg_off
            circle_x = self.height / 2
            circle_color = self.circle_off

        # Draw the track
        self.create_rounded_rect(
            0, 0, self.width, self.height, radius=self.height, fill=bg_color
        )

        # Draw the thumb/circle
        self.create_oval(
            circle_x - self.height / 2 + 2,
            2,
            circle_x + self.height / 2 - 2,
            self.height - 2,
            fill=circle_color,
            outline="",
        )

    def create_rounded_rect(self, x1, y1, x2, y2, radius=10, **kwargs):
        points = [
            x1 + radius,
            y1,
            x2 - radius,
            y1,
            x2,
            y1,
            x2,
            y1 + radius,
            x2,
            y2 - radius,
            x2,
            y2,
            x2 - radius,
            y2,
            x1 + radius,
            y2,
            x1,
            y2,
            x1,
            y2 - radius,
            x1,
            y1 + radius,
            x1,
            y1,
        ]
        return self.create_polygon(points, **kwargs, smooth=True)

    def toggle(self, event=None):
        self.state = not self.state
        self.draw_switch()

        if self.command:
            # Run the command in a separate thread
            threading.Thread(
                target=self.command, args=(self.state,), daemon=True
            ).start()

    def set_state(self, state):
        self.state = state
        self.draw_switch()


class App:
    def __init__(self, root):
        self.root = root
        self.root.title("内外网切换")
        self.root.geometry("300x200")

        # Create a style for the frame

        style = ttk.Style()
        style.configure("TFrame", background="#ffffff")

        print(style.theme_names())

        # Main frame
        main_frame = ttk.Frame(root, padding="20", style="TFrame")
        main_frame.pack(fill=tk.BOTH, expand=True)

        # # Label
        # label = ttk.Label(main_frame, text="内外网切换", font=("Arial", 12))
        # label.pack(pady=10)

        # Create the switch
        self.switch = SwitchButton(
            main_frame,
            width=80,
            height=36,
            fg_off="#e0e0e0",
            fg_on="#66BB6A",
            circle_off="#f5f5f5",
            circle_on="#ffffff",
            command=self.switch_action,
        )
        self.switch.pack(pady=20)

        # Status label
        self.status_label = ttk.Label(main_frame, text="未联网", font=("Arial", 10))
        self.status_label.pack()

        # Exit button
        exit_button = ttk.Button(main_frame, text="退出", command=self.quit_app)
        exit_button.pack(pady=10)

        self.swt = SwitcheNet()

    def quit_app(self):
        self.swt.quit()
        self.root.quit()

    def switch_action(self, state):
        # Update the UI immediately
        self.root.after(0, self.update_status_label, state)

        # Run the appropriate function in a new thread
        if state:
            threading.Thread(target=self.long_running_on_function, daemon=True).start()
        else:
            threading.Thread(target=self.long_running_off_function, daemon=True).start()

    def update_status_label(self, state):
        if state:
            self.status_label.config(text="切换外网中……")
        else:
            self.status_label.config(text="切换内网中……")

    def long_running_on_function(self):
        self.swt.change("互联网")

        # Simulate a long-running task
        for i in range(1, 4):
            time.sleep(1)
            # Update the UI through the main thread
            self.root.after(
                0,
                self.status_label.config,
                {"text": f"切换外网中…… ({i}/3)"},
            )

        self.root.after(0, self.status_label.config, {"text": "切换外网-成功"})

    def long_running_off_function(self):
        self.swt.change("玉溪市政务外网")

        # Simulate a long-running task
        for i in range(1, 4):
            time.sleep(1)
            # Update the UI through the main thread
            self.root.after(
                0,
                self.status_label.config,
                {"text": f"切换内网中…… ({i}/3)"},
            )

        self.root.after(0, self.status_label.config, {"text": "切换内网-成功"})


if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()
